package com.chart.web.controller.doubao;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.*;

@RestController
//@RequestMapping("/doubao/ai")
public class DoubaoAi {

    @Value("${doubao.api.url}") // API 地址
    private String doubaoApiUrl;

    @Value("${doubao.api.key}") // API Key
    private String doubaoApiKey;

    private final RestTemplate restTemplate = new RestTemplate();

    /**
     * 调用视觉理解大模型接口
     * @param request 请求体包含图片 URL 和理解深度
     * @return 大模型的响应
     */
    @PostMapping("/vision")
    public ResponseEntity<?> callVisualModel(@RequestBody VisualModelRequest request) {
        try {
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            headers.set("Authorization", "Bearer " + doubaoApiKey);

            // 构建请求体
            Map<String, Object> requestBody = new HashMap<>();
            requestBody.put("model", "ep-20241217154432-kkndg");

            List<Map<String, Object>> messages = new ArrayList<>();
            Map<String, Object> message = new HashMap<>();
            message.put("role", "user");

            // 构建内容
            List<Map<String, String>> content = new ArrayList<>();
            Map<String, String> textContent = new HashMap<>();
            textContent.put("type", "text");
            textContent.put("text", "图片主要讲了什么?");

            Map<String, String> imageContent = new HashMap<>();
            imageContent.put("type", "image_url");
            imageContent.put("image_url", request.getImageUrl()); // 使用传入的图片 URL

            content.add(textContent);
            content.add(imageContent);

            message.put("content", content);
            messages.add(message);

            requestBody.put("messages", messages);
            requestBody.put("detail", request.getDetail());

            // 发送 POST 请求
            HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(requestBody, headers);
            ResponseEntity<String> response = restTemplate.exchange(
                    doubaoApiUrl,
                    HttpMethod.POST,
                    requestEntity,
                    String.class
            );

            // 返回响应结果
            ObjectMapper objectMapper = new ObjectMapper();
            Map<?, ?> responseMap = objectMapper.readValue(response.getBody(), Map.class);
            return ResponseEntity.ok(responseMap);

        } catch (Exception e) {
            return ResponseEntity.status(500)
                    .body(Collections.singletonMap("error", "调用视觉理解API出错：" + e.getMessage()));
        }
    }






    // 请求体映射类
    public static class VisualModelRequest {
        private String imageUrl;
        private String detail;

        // Getters and Setters
        public String getImageUrl() {
            return imageUrl;
        }

        public void setImageUrl(String imageUrl) {
            this.imageUrl = imageUrl;
        }

        public String getDetail() {
            return detail;
        }

        public void setDetail(String detail) {
            this.detail = detail;
        }
    }
}
